package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.Book;
import com.ws.response.BookListResponse;
import com.ws.response.BookPageResponse;
import com.ws.response.BookResponse;
import com.ws.service.BookService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/22 21:59
 * @description：图书管理接口
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;

    /**
     * 书籍查询接口
     * @param pageable
     * @param bookName
     * @param author
     * @param publisher
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "书籍查询接口", notes = "书籍查询接口", httpMethod = "GET", response = BookPageResponse.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "bookName", value = "图书名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "author", value = "作者", dataType = "String", required = false),
            @ApiImplicitParam(name = "publisher", value = "出版社", dataType = "String", required = false)})
    public ResponseEntity<BookPageResponse> getBookPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="bookName",required=false) String bookName,
            @RequestParam(value="author",required=false) String author,
            @RequestParam(value="publisher",required=false) String publisher,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        PaasPage<Book> bookPaasPage = bookService.getBookPage(pageable,bookName,author,publisher,statusCd);
        return ResponseEntity.ok(new BookPageResponse(bookPaasPage));
    }

    /**
     * 书籍查询接口
     * @param bookName
     * @param author
     * @param publisher
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "书籍查询接口", notes = "书籍查询接口", httpMethod = "GET", response = BookListResponse.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "bookName", value = "图书名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "author", value = "作者", dataType = "String", required = false),
            @ApiImplicitParam(name = "publisher", value = "出版社", dataType = "String", required = false)})
    public ResponseEntity<BookListResponse> getBookList(
            @RequestParam(value="bookName",required=false) String bookName,
            @RequestParam(value="author",required=false) String author,
            @RequestParam(value="publisher",required=false) String publisher,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        List<Book> bookList = bookService.getBookList(bookName,author,publisher,statusCd);
        return ResponseEntity.ok(new BookListResponse(bookList));
    }

    /**
     * 书籍新增接口
     * @param book
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "书籍保存修改接口", notes = "书籍保存修改接口", httpMethod = "POST", response = BookResponse.class)
    public ResponseEntity<BookResponse> saveBook(@RequestBody @Valid Book book){
        Book book1 = bookService.saveBook(book);
        return ResponseEntity.ok(new BookResponse(book1));
    }

    /**
     * 查询对应书籍的基本信息
     * @param bookId
     * @return
     */
    @GetMapping("/{bookId}/basicInfo")
    @ApiOperation(value = "查询对应书籍的基本信息", notes = "查询对应书籍的基本信息", httpMethod = "GET", response = BookResponse.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "bookId", value = "图书标识", dataType = "Long", required = true)})
    public ResponseEntity<BookResponse> getBookBasicInfo(@PathVariable Long bookId){
        Book book = bookService.getBookBasicInfo(bookId);
        return ResponseEntity.ok(new BookResponse(book));
    }

    /**
     * 删除对应的书籍
     * @param bookId
     * @return
     */
    @PostMapping("/d/{bookId}")
    @ApiOperation(value = "删除对应的书籍", notes = "删除对应的书籍", httpMethod = "POST", response = PaasResponse.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "bookId", value = "图书标识", dataType = "Long", required = true)})
    public ResponseEntity delBook(@PathVariable Long bookId)  {
        bookService.delBook(bookId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的书籍
     * @param bookId
     * @return
     */
    @PostMapping("/u/{bookId}")
    @ApiOperation(value = "修改对应的书籍", notes = "修改对应的书籍", httpMethod = "POST", response = BookResponse.class)
    public ResponseEntity<BookResponse> updateBook(@PathVariable Long bookId,@RequestBody Book book)  {
        Book book1 = bookService.updateBook(bookId,book);
        return ResponseEntity.ok(new BookResponse(book1));
    }

}
